package controllers;

import java.io.IOException;
import java.net.URLDecoder;
import java.util.Date;
import java.util.TreeMap;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import models.OwnerDomainModel;

import entities.OwnerDomain;
import forms.OwnerDomainForm;

public class EditOwnerSiteServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException {
		TreeMap check = new TreeMap();
		String path = ((HttpServletRequest) request).getRequestURI();
		String type = URLDecoder.decode(path.toLowerCase().replaceAll("/profile/edit/", ""),"UTF-8");
		String domainName = (String) request.getParameter("domain");
		
		if (domainName == null || !(type.equalsIgnoreCase("title") || type.equalsIgnoreCase("description") || type.equalsIgnoreCase("status"))) {
			response.sendRedirect("/msg/page-not-found");
			return ;
		}
		
		OwnerDomainModel ownerData = OwnerDomainModel.getInstance();
		OwnerDomain ownerDomain = ownerData.getByDomain(domainName);
		
		if (ownerDomain == null) {
			response.sendRedirect("/msg/page-not-found");
			return ;
		}
		
		request.setAttribute("check", check);
		request.setAttribute("type", type);
		request.setAttribute("ownerDomain", ownerDomain);
		try {
			request.getRequestDispatcher("/pages/edit_owner_site.jsp").forward(request, response);
		} catch (ServletException e) {
			//e.printStackTrace();
		}
		
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException {
		String path = ((HttpServletRequest) request).getRequestURI();
		String type = URLDecoder.decode(path.toLowerCase().replaceAll("/profile/edit/", ""),"UTF-8");
		String domainName = (String) request.getParameter("domain");
		
		if (domainName == null || !(type.equalsIgnoreCase("title") || type.equalsIgnoreCase("description") || type.equalsIgnoreCase("status"))) {
			response.sendRedirect("/msg/page-not-found");
			return ;
		}
		
		OwnerDomainModel ownerData = OwnerDomainModel.getInstance();
		OwnerDomain ownerDomain = ownerData.getByDomain(domainName);
		
		if (ownerDomain == null) {
			response.sendRedirect("/msg/page-not-found");
			return ;
		}
		
		TreeMap form = new TreeMap();
		TreeMap check = new TreeMap();
		
		if (type.equalsIgnoreCase("title")) {
			form = OwnerDomainForm.checkValidTitle(request, ownerDomain);
			check = (TreeMap) form.get("checkValid");
			ownerDomain = (OwnerDomain) form.get("ownerDomain");
		} else if (type.equalsIgnoreCase("description")) {
			form = OwnerDomainForm.checkValidDescription(request, ownerDomain);
			check = (TreeMap) form.get("checkValid");
			ownerDomain = (OwnerDomain) form.get("ownerDomain");
		} else {
			form = OwnerDomainForm.checkValidActive(request, ownerDomain);
			check = (TreeMap) form.get("checkValid");
			ownerDomain = (OwnerDomain) form.get("ownerDomain");
		}
		
		if (check.size() == 0) {
			ownerDomain.setIsUpdated(0);
			ownerData.insert(ownerDomain);
			response.sendRedirect("/preview/" + ownerDomain.getDomain());
			return;
		}
		
		request.setAttribute("check", check);
		request.setAttribute("type", type);
		request.setAttribute("ownerDomain", ownerDomain);
		try {
			request.getRequestDispatcher("/pages/edit_owner_site.jsp").forward(request, response);
		} catch (ServletException e) {
			//e.printStackTrace();
		}
	}
}
